$(function(){
	var bwTel = $('#bw-tel');
	var bjTel = $('#bj-tel');
	var zbTel = $('#zb-tel');
	var zbTel = $('#zb-tel');
	var xlName = $('#xl-name');
	var mwName = $('#mw-name');
	var bjName1 = $('#bj-name1');
	var bjName2 = $('#bj-name2');
	var bjName3 = $('#bj-name3');
	var bjName4 = $('#bj-name4');
	var xcName = $('#xc-name');
	var xcTel = $('#xc-tel');
	var cgTel = $('#cg-tel');
	var cgName = $('#cg-name');
	var csName = $('#cs-name');
	var csTel = $('#cs-tel');
	var wxName = $('#wx-name');
	var wxTel = $('#wx-tel');
	var tableNameArr = ['部门管理','员工','伙食','teaInform'];
	
	/* 数据加载部分 */
	var json = JSON.parse(localStorage.getItem('部门管理'));
	var arrYg = JSON.parse(localStorage.getItem('员工')) ;
	var arrFood = JSON.parse(localStorage.getItem('伙食')) ;
	var arrtec = JSON.parse(localStorage.getItem('teaInform'));
	
	
	function render (){
		var arrDuty = [];
		
		//食物表
		$('.food-table table tbody').html('')
		$('.duty-table table tbody tr').html('')
		$.each(arrFood,function(i,v){
			$('<tr>'
			+'<td><input type="text" value="'+v.day1food+'"></td>'
			+'<td><input type="text" value="'+v.day2food+'"></td>'
			+'<td><input type="text" value="'+v.day3food+'"></td>'
			+'<td><input type="text" value="'+v.day4food+'"></td>'
			+'<td><input type="text" value="'+v.day5food+'"></td></tr>').appendTo($('.food-table table tbody'))
		});
		//值班表
		
		$.each(arrtec,function(i,v){
			if(v.isDuty == 'yes'){
				arrDuty.push(v);
			}	
		})
		for(var j=1;j<4;j++){
			if(j==1){
				$('<td><i class="fa fa-sun-o" aria-hidden="true"></i></td>'
					+'<td><i class="fa fa-sun-o" aria-hidden="true"></i></td>'
					+'<td><i class="fa fa-sun-o" aria-hidden="true"></i></td>'
					+'<td><i class="fa fa-sun-o" aria-hidden="true"></i></td>'
					+'<td><i class="fa fa-sun-o" aria-hidden="true"></i></td>').appendTo($('.duty-table table tbody tr:nth-child('+j+')'))
			}else if(j==3){
				$('<td><i class="fa fa-moon-o" aria-hidden="true"></i></td>'
					+'<td><i class="fa fa-moon-o" aria-hidden="true"></i></td>'
					+'<td><i class="fa fa-moon-o" aria-hidden="true"></i></td>'
					+'<td><i class="fa fa-moon-o" aria-hidden="true"></i></td>'
					+'<td><i class="fa fa-moon-o" aria-hidden="true"></i></td>').appendTo($('.duty-table table tbody tr:nth-child('+j+')'))
			}else {
				for(var k=0;k<arrDuty.length;k++){
					if(k<5){
						$('<td><input type="text" value="'+arrDuty[k].name+'"></td>').appendTo($('.duty-table table tbody tr:nth-child('+2+')'))
					}else {
						$('<td><input type="text" value="'+arrDuty[k].name+'"></td>').appendTo($('.duty-table table tbody tr:nth-child('+4+')'))
					}
				}
				
			}
		}
		
		//food
		
		
		
		//各部门电话
		var bw = $('.tel-num3 input');
		var oSpan = $('.tel-num6 input');
		$.each(json,function(i,v){
			if(v.dept == '保卫部'){
				bw[0].value = v.tel;
			}
			if(v.dept == '后勤部'){
				bw[1].value = v.tel;
			}
			if(v.dept == '教务部'){
				bw[2].value = v.tel;
			}
			
		})
		var j = 2;
		$.each(arrYg,function(i,v){
			if(v.position == '巡逻'){
				$('.tel-num6 input').eq(0).val(v.name)
			}
			if(v.position == '保安'){
				$('.tel-num6 input').eq(1).val(v.name)
			}
			
			if(v.position == '保洁员'){
				if(j<6&&j>=2){
					$('.tel-num6 input').eq(j).val(v.name)
					j++;
				}	
			}
			if(v.position == '司机'){
				$('.tel-num6 input').eq(6).val(v.name)
				$('.tel-num6 input').eq(7).val(v.tel)
			}
			
			if(v.position == '采购员'){
				$('.tel-num6 input').eq(8).val(v.name)
				$('.tel-num6 input').eq(9).val(v.tel)
			}
			if(v.position == '厨师'){
				$('.tel-num6 input').eq(10).val(v.name)
				$('.tel-num6 input').eq(11).val(v.tel)
			}
			
			if(v.position == '维修工'){
				$('.tel-num6 input').eq(12).val(v.name)
				$('.tel-num6 input').eq(13).val(v.tel)
			}

		});
		
		localStorage.setItem('员工',JSON.stringify(arrYg));
		localStorage.setItem('伙食',JSON.stringify(arrFood));
		localStorage.setItem('teaInform',JSON.stringify(arrtec));
		localStorage.setItem('部门管理',JSON.stringify(json));
	}

	if(arrFood.length){
		render()
	}
	
	
	$('.right-side').on('click','.modify',function(){
		var tf = confirm('确定修改？')
		var telNum3 = $(this).parents('.boxs').find('.hq-title').text();
		if(telNum3 == '保安室联系电话'){
			$.each(json,function (i,e) {
				if(e.dept == '保卫部'){
					var flag1 = verify(bwTel,/^1\d{10}$/,'电话只能为1开头的11位个数字');
					if(flag1&&tf){
						e.tel = parseInt(bwTel.val()) 
						render()
					}
				}	
			})
		}else if(telNum3 == '保洁室联系电话'){
			$.each(json,function (i,e) {
				if(e.dept == '后勤部'){
					var flag1 = verify(bjTel,/^1\d{10}$/,'电话只能为1开头的11位个数字');
					if(flag1&&tf){
						e.tel = parseInt(bjTel.val()) 
						render()
					}
				}	
			})		
		}else if(telNum3 == '值班室联系电话'){
			$.each(json,function (i,e) {
				if(e.dept == '教务部'){
					var flag1 = verify(zbTel,/^1\d{10}$/,'电话只能为1开头的11位个数字');
					if(flag1&&tf){
						e.tel = parseInt(zbTel.val()) 
						render()
					}
				}	
			})		
		}else if(telNum3 == '今日在职保安人员'){
			$.each(arrYg,function (i,e) {
				if(e.dept == '保卫部'&& e.position == '巡逻'){
					var flag1 = verify(xlName,/^[\u4e00-\u9fa5]{2,5}$/,'名字只能为2-5个中文');
					if(flag1&&tf){
						e.name = xlName.val()
						render()
					}
				}else if(e.dept == '保卫部'&& e.position == '保安'){
					var flag2 = verify(mwName,/^[\u4e00-\u9fa5]{2,5}$/,'名字只能为2-5个中文');
					if(flag2&&tf){
						e.name = mwName.val() 
						render()
					}
				}	
			})		
		}else if(telNum3 == '今日在职保洁人员'){
			var arrbj = []
			var arrbjyg = [bjName1.val(),bjName2.val(),bjName3.val(),bjName4.val()]
			var arrbjyg2 = [bjName1,bjName2,bjName3,bjName4]
			$.each(arrYg,function (i,e) {
				if(e.position=='保洁员'){
					arrbj.push(e.name);
				}
				if(e.dept == '后勤部'&& e.position == '保洁员' && e.name == arrbj[0]){
					var flag1 = verify(bjName1,/^[\u4e00-\u9fa5]{2,5}$/,'名字只能为2-5个中文');
					if(flag1&&tf){
						e.name = bjName1.val();
						console.log(e.name)
						render()
					}
				}else if(e.dept == '后勤部'&& e.position == '保洁员'&& e.name==arrbj[1]){
					var flag2 = verify(bjName2,/^[\u4e00-\u9fa5]{2,5}$/,'名字只能为2-5个中文');
					if(flag2&&tf){
						e.name = bjName2.val() 
						render()
					}
				}else if(e.dept == '后勤部'&& e.position == '保洁员'&& e.name==arrbj[2]){
					var flag3 = verify(bjName3,/^[\u4e00-\u9fa5]{2,5}$/,'名字只能为2-5个中文');
					if(flag3&&tf){
						e.name = bjName3.val() 
						render()
					}
				}else if(e.dept == '后勤部'&& e.position == '保洁员'&& e.name==arrbj[3]){
					var flag4 = verify(bjName4,/^[\u4e00-\u9fa5]{2,5}$/,'名字只能为2-5个中文');
					if(flag4&&tf){
						e.name = bjName4.val() 
						render()
					}
				}	
			})
		}else if(telNum3 == '今日在职校车人员'){
			$.each(arrYg,function (i,e) {
				if(e.dept == '后勤部'&& e.position == '司机'){
					var flag1 = verify(xcName,/^[\u4e00-\u9fa5]{2,5}$/,'名字只能为2-5个中文');
					var flag2 = verify(xcTel,/^1\d{10}$/,'电话只能为1开头的11位个数字');
					if(flag1&&tf&&flag2){
						e.name = xcName.val()
						e.tel = xcTel.val()
						render()
					}
				}
			})		
		}else if(telNum3 == '今日在职采购人员'){
			$.each(arrYg,function (i,e) {
				if(e.dept == '伙食部'&& e.position == '采购员'){
					var flag1 = verify(cgName,/^[\u4e00-\u9fa5]{2,5}$/,'名字只能为2-5个中文');
					var flag2 = verify(cgTel,/^1\d{10}$/,'电话只能为1开头的11位个数字');
					if(flag1&&tf&&flag2){
						e.name = cgName.val()
						e.tel = cgTel.val()
						render()
					}
				}
			})		
		}else if(telNum3 == '今日在职食堂人员'){
			$.each(arrYg,function (i,e) {
				if(e.dept == '伙食部'&& e.position == '厨师'){
					var flag1 = verify(csName,/^[\u4e00-\u9fa5]{2,5}$/,'名字只能为2-5个中文');
					var flag2 = verify(csTel,/^1\d{10}$/,'电话只能为1开头的11位个数字');
					if(flag1&&tf&&flag2){
						e.name = csName.val()
						e.tel = csTel.val()
						render()
					}
				}
			})		
		}else if(telNum3 == '今日在职维修人员'){
			$.each(arrYg,function (i,e) {
				if(e.dept == '后勤部'&& e.position == '维修工'){
					var flag1 = verify(wxName,/^[\u4e00-\u9fa5]{2,5}$/,'名字只能为2-5个中文');
					var flag2 = verify(wxTel,/^1\d{10}$/,'电话只能为1开头的11位个数字');
					if(flag1&&tf&&flag2){
						e.name = wxName.val()
						e.tel = wxTel.val()
						render()
					}
				}
			})		
		}
		
	});
	$('.food-table .table span .modify').on('click',function(){
		var tf1 = confirm('确定修改？');
		var foodIpt = $('.food-table tbody tr:eq(0) input')
		var foodIpt2 = $('.food-table tbody tr:eq(1) input')
		var foodIpt3 = $('.food-table tbody tr:eq(2) input')
			if(tf1){
				arrFood[0].day1food = foodIpt[0].value;
				arrFood[0].day2food = foodIpt[1].value;
				arrFood[0].day3food = foodIpt[2].value;
				arrFood[0].day4food = foodIpt[3].value;
				arrFood[0].day5food = foodIpt[4].value;
				arrFood[1].day1food = foodIpt2[0].value;
				arrFood[1].day2food = foodIpt2[1].value;
				arrFood[1].day3food = foodIpt2[2].value;
				arrFood[1].day4food = foodIpt2[3].value;
				arrFood[1].day5food = foodIpt2[4].value;
				arrFood[2].day1food = foodIpt3[0].value;
				arrFood[2].day2food = foodIpt3[1].value;
				arrFood[2].day3food = foodIpt3[2].value;
				arrFood[2].day4food = foodIpt3[3].value;
				arrFood[2].day5food = foodIpt3[4].value;
				render()
			}
			
	})
	
	// $('.duty-table .modify').on('click',function(){
	// 	var tf1 = confirm('确定修改？');
	// 	var dutyIpt = $('.duty-table tbody tr:eq(1) input');
	// 	var arrdu = []
	// 	for(var i=0;i<dutyIpt.length;i++){
	// 		$.each(arrtec,function (index,element) {
	// 			if(dutyIpt[i].value == el)
	// 		})
			
	// 	}
		
	// })
	
	function verify(ipt,reg,str){
		var res;
		var arr = [];
		
		ipt.focusout(res = function(){
			if(!reg.test(ipt.val())){
				ipt.next().text(str);
				return  false;
			}
			else {
				ipt.next().text('');
				return true;
			}
		});
		return res();
	};
	
})

